<?php

class Chat_topic extends Frontend_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('chat_topic_m');
        $this->load->model('thread_m');
        $this->load->model('reply_m');
        $this->load->library("pagination");
        $this->load->library('Breadcrumbs');
    }

    public function index() {
        $this->breadcrumbs->push('Topics', site_url('chat_topic'));
        $this->breadcrumbs->push('Threads', site_url('chat_topic/view_thread'));
        $data['title'] = "Welcome to our Furm";
        $this->data['breackcrumb'] = $this->breadcrumbs->show();
        $this->paginate_record_topics();
    }

    public function view_thread($id = NULL) {
        $this->breadcrumbs->push('Topics', site_url('chat_topic'));
        $this->breadcrumbs->push('Threads', site_url('chat_topic/view_thread/' . $id));
        $this->breadcrumbs->push('Reply', site_url('chat_topic/view_thread/reply_view'));

        $this->data['breackcrumb'] = $this->breadcrumbs->show();
        $this->paginate_record_threads($id);
    }

    public function reply_view($id = NULL) {
        $this->breadcrumbs->push('Topics', site_url('chat_topic'));
        $this->breadcrumbs->push('Threads', site_url('chat_topic/view_thread'));
        $this->breadcrumbs->push('Reply', site_url('chat_topic/view_thread/reply_view'));
        $this->data['breackcrumb'] = $this->breadcrumbs->show();
        $this->data['result'] = $this->chat_topic_m->get_thread($id);
        $this->paginate_record_reply($id);
    }

// paginate and get all record from tbl replies			
    public function paginate_record_reply($id) {
        $config = array();
        $config["base_url"] = site_url('chat_topic/reply_view/');
        $config["total_rows"] = $this->reply_m->record_count($id);
        $config["per_page"] = 4;
        $config["uri_segment"] = 4;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        $this->data["all_reply"] = $this->reply_m->fetch_reply($config["per_page"], $page);
        $this->data["links"] = $this->pagination->create_links();

        $this->load->view('frontend/reply_v', $this->data);
    }

// paginate and get all record from tbl Thread			
    public function paginate_record_threads($id) {
        $config = array();
        $config["base_url"] = site_url('chat_topic/index/');
        $config["total_rows"] = $this->thread_m->record_count($id);
        $config["per_page"] = 4;
        $config["uri_segment"] = 4;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        $this->data["results"] = $this->thread_m->fetch_topic($config["per_page"], $page);
        $this->data["links"] = $this->pagination->create_links();

        $this->load->view('frontend/thread_v', $this->data);
    }

// paginate and get all record from tbl Topic	
    public function paginate_record_topics() {
        $config = array();
        $config["base_url"] = site_url('chat_topic/index/');
        $config["total_rows"] = $this->chat_topic_m->record_count();
        $config["per_page"] = 4;
        $config["uri_segment"] = 4;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

        $this->data["results"] = $this->chat_topic_m->fetch_topic($config["per_page"], $page);
        $this->data["links"] = $this->pagination->create_links();
        $this->load->view('frontend/index', $this->data);
    }

    function insert_re() {
        $this->load->model('chat_topic_m');
        $authorID = $this->chat_topic_m->getAuthorID($this->session->userdata('user_login'));
        $thread_id = $this->input->post('thread_id');
        $dated_replied = date("Y-m-d");
        $data['re'] = $this->chat_topic_m->reply_to($authorID, $thread_id, $dated_replied);

        if ($data) {
            $this->load->view('thank_you');
        } else {
            echo "Error";
        }
    }

}